package com.company.Dynamic;

public class Frog {

    int[] river;
    StringBuilder riverStr;
    StringBuilder indexStr;
    private void buildRiver(int[] stones){
        int lastStone = stones[stones.length-1];
        river = new int[lastStone+1];

        river[0]=1;

        for (int i = 1; i < stones.length; i++) {
            river[stones[i]]=1;
        }
        riverStr = new StringBuilder();
        for (int i = 0; i < river.length; i++) {
            riverStr.append(river[i]==0?"_ ":"= ");
        }
        indexStr=new StringBuilder();
        for (int i = 0; i < river.length; i++) {
            if (i<10) {
                indexStr.append(" ");
            }
            indexStr.append(i);
        }
    }

    public boolean canCross(int[] stones) {

        buildRiver(stones);
       return jump(river,0,1);
    }


    public boolean jump(int[] river,int curPosition,int curSteps){
        if (curPosition>=river.length) {
            return false;
        }
        drawState(curPosition);

        if (curPosition == river.length-1) {
            return true;
        }

        for (int i = -1; i < 2; i++) {
            int nextStep = curSteps+i;
            int nextPosition = curPosition+nextStep;

            if (nextPosition<river.length&&nextStep>0&&river[nextPosition]==1) {
                boolean reached = jump(river, nextPosition, nextStep);
                if (reached) {
                    return true;
                }
            }

        }
        return false;
    }
    private void drawState(int position){
        StringBuilder frog=new StringBuilder();
        for (int i = 0; i < position; i++) {
            frog.append("  ");
        }
        frog.append("e");
        System.out.println(frog.toString());
        System.out.println(riverStr.toString());
//        System.out.println(indexStr.toString());
    }

}
